package com.hd.crdao.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.bean.MsgBean;
import com.hd.crdao.config.Config;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/7/27 0027
 */

public class CarBuyDeatailActivity extends Activity {

    public WebView webView;
    public String param;
    private String sendDatas;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cardetails);
        initView();
    }

    protected void initView() {
        param = getIntent().getStringExtra("param");
        webView = (WebView) findViewById(R.id.webview);
        setSettings(webView);
        webView.loadUrl("file:///android_asset/html/car_buy_con.html");
        webView.addJavascriptInterface(new ProviderForJavaScript(), "connectedBridge");

    }


    @SuppressLint("SetJavaScriptEnabled")
    private void setSettings(WebView webView) {
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setAppCacheEnabled(false);
        webView.setWebChromeClient(new WebChromeClient());
    }


    private class ProviderForJavaScript {
        @JavascriptInterface
        public void sendParam() {
            webView.post(new Runnable() {
                @Override
                public void run() {
                    ALog.d(param);
                    webView.loadUrl("javascript:vm.init(" + param + ")");
                }
            });
        }

        @JavascriptInterface
        public void getParam(String param) {
            String businessid = SPUtils.getString(Config.businessId, "");

            String url = Constants.BUYNEWCAR_URL;
            String data = businessid + "~" + param;
            ALog.d("访问服务器的数据:" + data);
            OkHttpUtils.post().url(url)
                    .addParams("sign", Constants.SIGN)
                    .addParams("data", data)
                    .build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    MyToast.show("网络开了个小差");
                }

                @Override
                public void onResponse(String response, int id) {
                    MsgBean msgBean = new Gson().fromJson(response, MsgBean.class);
                    if (msgBean.getSuccess() == 0) {
                        MyToast.show(msgBean.getMessage());
                        startActivity(new Intent(UIUtils.getContext(), NewOrderActivity.class));
                        finish();
                    } else {
                        MyToast.show(msgBean.getMessage());
                    }
                }
            });

        }


        @JavascriptInterface
        public void goBackPressed() {
            onBackPressed();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView != null) {
            webView.removeJavascriptInterface("connectedBridge");
            webView.destroy();
        }
    }
}
